var fs = require('fs');
const path = require("path");
var join = require('path').join;
let jsonFiles = [];

function findJsonFile(path) {
    let files = fs.readdirSync(path);
    files.forEach(function (item, index) {
        let fPath = join(path, item);
        let stat = fs.statSync(fPath);
        if (stat.isFile() === true) {
            jsonFiles.push(fPath);
        }
    });
}

function run(jsonPath) {
    console.log("create...");
    findJsonFile(jsonPath)
    let names = jsonFiles.map((item) => {
        let dotIndex = item.lastIndexOf('.')
        let slantingIndex = item.lastIndexOf('/')
        let varname = item.substr(slantingIndex + 1, dotIndex - slantingIndex - 1);
        var fileName=item.substr(item.lastIndexOf('/')+1,item.length)
        if (!varname) return ''
        return ` static String ${varname.replace('-', '_')} = g("${fileName}");`
    }).filter(item => !!item);
    fs.writeFile(
        path.resolve(__dirname, "../lib/resources/images/images.dart"),
        `String g(String str) => 'assets/images/' + str;\nclass ImageAssets {\n${names.join("\n")}\n}`,
        function (err) {
            if (err) {
                throw new Error(err);
            } else {
                console.log("create image map successe.");
            }
        }
    );
}

run(path.resolve(__dirname, "../assets/images"));